********Merge lifetime variables and clean IFLS data for analysis********

args ifls45

use "`ifls45'", clear

drop if ifls == .
drop if pidlink == ""

*Merge lifetime macroeconomic experiences
merge m:1 pidlink using "$reploc/data/created_data/indiv_provgrbir_stats.dta", gen(_mergeprovbrtseries)

*Merge lifetime macroeconomic experiences, from state of residence
merge m:1 pidlink using "$reploc/data/created_data/indiv_provgrres_stats.dta", gen(_mergeprovresseries)


forval lambda=0(10)0 {
local lam100=round(`lambda')

if `lambda'<0 {
local hola=substr("`lam100'",2,.)
local lamnam neg`hola'
}
else {
local lamnam `lam100'
}

local provavgs `provavgs' grbrprovAvg_lam`lamnam' grbrprovSD_lam`lamnam' grexprovAvg_lam`lamnam' grexprovSD_lam`lamnam'

}

foreach v in `provavgs' {
gen `v' = .
replace `v' = `v'_7707 if ifls == 4
replace `v' = `v'_7714 if ifls == 5

gen `v'co2 = .
replace `v'co2 = `v'_7707co2 if ifls == 4
replace `v'co2 = `v'_7714co2 if ifls == 5

gen `v'co3 = .
replace `v'co3 = `v'_7707co3 if ifls == 4
replace `v'co3 = `v'_7714co3 if ifls == 5

}

foreach v in `provavgs' {

replace `v' = . if biryear_impl<1961 // as otherwise growth variables are not properly constructed
replace `v'co2  = . if biryear_impl<1961 // as otherwise growth variables are not properly constructed
replace `v'co3  = . if biryear_impl<1961 // as otherwise growth variables are not properly constructed

}

*Demographics

gen age=.
replace age=IFLS4_age if ifls==4
replace age=IFLS5_age if ifls==5
la var age "Age"
drop if age==999

gen imp_age = .
replace imp_age = 2007 - biryear_impl if ifls == 4 
replace imp_age = 2014 - biryear_impl if ifls == 5
drop if imp_age <= 12

gen imp_age_sq = .
replace imp_age_sq = imp_age^2

gen sex=.
replace sex=IFLS4_sex if ifls==4
replace sex=IFLS5_sex if ifls==5

gen woman = .
replace woman = 1 if sex == 3
replace woman = 0 if sex == 1

gen married = .
replace married = IFLS4_marstat if ifls==4
replace married = IFLS5_marstat if ifls==5
replace married = 0 if married == 1 | married == 6
replace married = 1 if married != 0 & married != .
la var married "Married"

gen muslim = .
replace muslim = IFLS4_tr12 if ifls==4
replace muslim = IFLS5_tr12 if ifls==5
replace muslim = . if muslim == 96
replace muslim = 0 if muslim != 1 & muslim != .
la var muslim "Religion (1: Muslim; 0: Non-Muslim)"

gen religiosity = . 
replace religiosity = IFLS4_tr11 if ifls==4
replace religiosity = IFLS5_tr11 if ifls==5 
replace religiosity = . if religiosity == 7 | religiosity == 9
la var religiosity "How religious are you? (1-4, 1 highest)"

gen educlevel=.
replace educlevel=IFLS4_dl06 if ifls==4
replace educlevel=IFLS5_dl06 if ifls==5

gen edlevel=.
replace edlevel = 1 if educlevel == 2 | educlevel == 72
replace edlevel = 2 if educlevel == 3 | educlevel == 4 | educlevel == 73
replace edlevel = 3 if educlevel == 5 | educlevel == 6 | educlevel == 74
replace edlevel = 4 if educlevel == 60 | educlevel == 61 | educlevel == 62 ///
	| educlevel == 63 | educlevel ==11 | educlevel ==12 | educlevel ==13 ///
	| educlevel ==14 | educlevel ==15  // Post-Secondary
la var edlevel "Highest education level attained (1 elem, 2 jun, 3 high, 4 coll)"

quietly tab edlevel, gen (Eddum)
la var Eddum1 "Comp. Elementary"
la var Eddum2 "Comp. Junior High"
la var Eddum3 "Comp. High School"
la var Eddum4 "Above High School"


gen hhsize = .	//generating household size variable
replace hhsize = IFLS4_hhsize if ifls==4
replace hhsize = IFLS5_hhsize if ifls==5
la var hhsize "Household Size"

gen hhsize_sq=.
replace hhsize_sq = IFLS4_hhsize^2 if ifls==4
replace hhsize_sq = IFLS5_hhsize^2 if ifls==5
la var hhsize_sq "Household Size Squared"

*Work information
gen workcat=.
replace workcat=IFLS4_tk24a if ifls==4 
replace workcat=IFLS5_tk24a if ifls==5 

gen entrep=.
replace entrep = 1 if workcat==1 | workcat==2 | workcat==3
replace entrep = 0 if workcat==4 | workcat==5 | workcat==6 | workcat==7 | workcat==8
replace entrep = . if workcat == 9
la var entrep "Self-employed"

gen employed=.
replace employed=1 if (IFLS4_tk02==1 | IFLS4_tk03==1 | IFLS4_tk04==1) & ifls==4
replace employed=0 if (IFLS4_tk02==3 & IFLS4_tk03==3 & IFLS4_tk04==3) & ifls==4

gen unemployed=.
replace unemployed=1 if (employed==0 & IFLS4_tk01==2) & ifls==4
replace unemployed=0 if (employed==1 | IFLS4_tk01!=2) & ifls==4
replace unemployed=1 if (employed==0 & IFLS5_tk01==2) & ifls==5
replace unemployed=0 if (employed==1 | IFLS5_tk01!=2) & ifls==5

gen workcat_an=.
replace workcat_an=1 if workcat==1 | workcat==2 | workcat==3
replace workcat_an=2 if workcat==4
replace workcat_an=3 if workcat==5
replace workcat_an=4 if workcat==6
replace workcat_an=5 if workcat==7
replace workcat_an=6 if workcat==8

replace IFLS4_tk19ab="" if IFLS4_tk19ab==" 0"

destring IFLS4_tk19ab, replace

gen industry=.
replace industry=IFLS4_tk19ab if ifls==4
replace industry=IFLS5_tk19ab if ifls==5

/*
CODE TK19Ab
Agriculture, forestry, fishing and hunting	01
Mining and quarrying	02
Manufacturing	03
Electricity, gas, water	04
Construction	05 	
Wholesale, retail, restaurants and hotels	06
Transportation, storage and communications	07
Finance, insurance, real estate and business services	08
Social services	09
Activities that cannot be classified	10  
*/

*Cash crop information

gen cashcropvalfirst=.
replace cashcropvalfirst=0 if IFLS4_ut00a==1 & ifls==4
replace cashcropvalfirst=0 if IFLS5_ut00a==1 & ifls==5
replace cashcropvalfirst=1 if ((IFLS4_ut07a==14 | IFLS4_ut07a==15 | IFLS4_ut07a==12 | IFLS4_ut07a==13 | IFLS4_ut07a==7) | (IFLS4_ut07a==14 | IFLS4_ut07a==15 | IFLS4_ut07a==9 | IFLS4_ut07a==12 | IFLS4_ut07a==13 | IFLS4_ut07a==7)) & ifls==4
replace cashcropvalfirst=1 if ((IFLS5_ut07a==14 | IFLS5_ut07a==15 | IFLS5_ut07a==12 | IFLS5_ut07a==13 | IFLS5_ut07a==7) | (IFLS5_ut07a==14 | IFLS5_ut07a==15 | IFLS5_ut07a==9 | IFLS5_ut07a==12 | IFLS5_ut07a==13 | IFLS5_ut07a==7)) & ifls==5
la var cashcropvalfirst "HH Reports a cash crop as most valuable crop you grow"

gen cashcropvalsecond=.
replace cashcropvalsecond=0 if IFLS4_ut00a==1 & ifls==4
replace cashcropvalsecond=0 if IFLS5_ut00a==1 & ifls==5
replace cashcropvalsecond=1 if ((IFLS4_ut07b==14 | IFLS4_ut07b==15 | IFLS4_ut07b==12 | IFLS4_ut07b==13 | IFLS4_ut07b==7) | (IFLS4_ut07b==14 | IFLS4_ut07b==15 | IFLS4_ut07b==9 | IFLS4_ut07b==12 | IFLS4_ut07b==13 | IFLS4_ut07b==7)) & ifls==4
replace cashcropvalsecond=1 if ((IFLS5_ut07b==14 | IFLS5_ut07b==15 | IFLS5_ut07b==12 | IFLS5_ut07b==13 | IFLS5_ut07b==7) | (IFLS5_ut07b==14 | IFLS5_ut07b==15 | IFLS5_ut07b==9 | IFLS5_ut07b==12 | IFLS5_ut07b==13 | IFLS5_ut07b==7)) & ifls==5
la var cashcropvalsecond "HH Reports a cash crop as second most valuable crop you grow"

gen cashcroprest=.
replace cashcroprest=0 if IFLS4_ut00a==1 & ifls==4
replace cashcroprest=0 if IFLS5_ut00a==1 & ifls==5
replace cashcroprest=1 if (strpos(IFLS4_ut07c,"N")!=0 | strpos(IFLS4_ut07c,"O")!=0 | strpos(IFLS4_ut07c,"l")!=0 | strpos(IFLS4_ut07c,"M")!=0 | strpos(IFLS4_ut07c,"G")!=0) & ifls==4
replace cashcroprest=1 if (strpos(IFLS5_ut07c,"N")!=0 | strpos(IFLS5_ut07c,"O")!=0 | strpos(IFLS5_ut07c,"l")!=0 | strpos(IFLS5_ut07c,"M")!=0 | strpos(IFLS5_ut07c,"G")!=0) & ifls==5
la var cashcropvalsecond "HH Reports growing cash crop, not first or second most valuable"


gen cashcropyesno=.
replace cashcropyesno=0 if IFLS4_ut00a==1 & ifls==4
replace cashcropyesno=0 if IFLS5_ut00a==1 & ifls==5
replace cashcropyesno=1 if cashcropvalfirst==1 | cashcropvalsecond==1 | cashcroprest==1
replace cashcropyesno=0 if cashcropyesno==. 
la var cashcropyesno "HH Reports growing cash crops"

*Smoking
gen eversmoke = .
replace eversmoke = IFLS4_km01a if ifls == 4
replace eversmoke = IFLS5_km01a if ifls == 5

gen cursmoke = .
replace cursmoke = IFLS4_km04 if ifls == 4
replace cursmoke = IFLS5_km04 if ifls == 5

gen smoke = .
replace smoke = 1 if cursmoke == 1
replace smoke = 0 if cursmoke == 3 | eversmoke == 3
la var smoke "Currently smoke"

gen smoketot_ind = .
replace smoketot_ind = IFLS4_km08x if ifls==4
replace smoketot_ind = IFLS5_km08x if ifls==5

gen smoketot = .
replace smoketot=IFLS4_km08 if ifls==4 & smoketot_ind == 1 & cursmoke == 1
replace smoketot=IFLS5_km08 if ifls==5 & smoketot_ind == 1 & cursmoke == 1
replace smoketot = 0 if smoke == 0
la var smoketot "# cigs/cigars smoked per day"

*Subjective well-being
gen SWlivingstandard=.
replace SWlivingstandard = IFLS5_sw04 if ifls==5
replace SWlivingstandard = IFLS4_sw04 if ifls==4
replace SWlivingstandard = . if SWlivingstandard == 8
la var SWlivingstandard "Assessment of current living standard	1 less than adequate, 2 just adequate, more than adecuate"

gen SWfoodconsumption=.
replace SWfoodconsumption = IFLS5_sw05 if ifls==5
replace SWfoodconsumption = IFLS4_sw05 if ifls==4
replace SWfoodconsumption = . if SWlivingstandard == 8
la var SWfoodconsumption "Assessment of food consumption	1 less than adequate, 2 just adequate, more than adecuate"

gen swlowlivingstandard=.
replace swlowlivingstandard=1 if SWlivingstandard==1
replace swlowlivingstandard=0 if SWlivingstandard==2 | SWlivingstandard==3

gen swlowfoodconsumption=.
replace swlowfoodconsumption=1 if SWfoodconsumption==1
replace swlowfoodconsumption=0 if SWfoodconsumption==2 | SWfoodconsumption==3

*Natural disaster and violence experiences
gen safe_village=.
replace safe_village=IFLS4_tr06 if ifls==4
replace safe_village=IFLS5_tr06 if ifls==5
la var safe_village "How safe do you consider village (1-very safe, 4-very unsafe)"

gen walk_village=.
replace walk_village=IFLS4_tr07 if ifls==4
replace walk_village=IFLS5_tr07 if ifls==5
la var walk_village "Is it safe for you to walk village alone at night (1-very safe, 4-very unsafe)"

gen civil_strife=0
replace civil_strife=strpos(IFLS4_nd01,"J") if ifls==4
replace civil_strife=strpos(IFLS5_nd01,"J") if ifls==5

gen civil_strife_bad=0
replace civil_strife_bad=0 if IFLS4_nd02==3 & ifls==4 & civil_strife==1
replace civil_strife_bad=0 if IFLS5_nd02==3 & ifls==5 & civil_strife==1
replace civil_strife_bad=1 if IFLS4_nd02==1 & ifls==4 & civil_strife==1
replace civil_strife_bad=1 if IFLS5_nd02==1 & ifls==5 & civil_strife==1
replace civil_strife_bad=. if civil_strife>1 & civil_strife_bad==1

replace civil_strife=1 if civil_strife>1


***Add at HH level
egen civil_strife_hh4=max(civil_strife) if ifls==4, by(IFLS4_hhid07)
egen civil_strife_hh5=max(civil_strife) if ifls==5, by(IFLS5_hhid14)

egen civil_strifebad_hh4=max(civil_strife_bad) if ifls==4, by(IFLS4_hhid07)
egen civil_strifebad_hh5=max(civil_strife_bad) if ifls==5, by(IFLS5_hhid14)

gen civil_strife_hh=.
replace civil_strife_hh=civil_strife_hh4 if ifls==4
replace civil_strife_hh=civil_strife_hh5 if ifls==5

gen civil_strifebad_hh=.
replace civil_strifebad_hh=civil_strifebad_hh4 if ifls==4
replace civil_strifebad_hh=civil_strifebad_hh5 if ifls==5

drop civil_strife_hh? civil_strifebad_hh?

la var civil_strife_hh "HH experienced civil strife in the past 5 years"
la var civil_strifebad_hh "Was civil strife bad enough"

local lettersdisasters A B C D E F G H I 
local disasternoms Flood Landslide Mudflow Volcanic_Eruption Earthquake Tsunami Windstorm Forest_Fire 

local nv : word count `lettersdisasters'

forval i=1/`nv' {

local let: word `i' of `lettersdisasters'
local disas: word `i' of `disasternoms'


gen dis_`disas'=0
replace dis_`disas'=strpos(IFLS4_nd01,"`let'") if ifls==4
replace dis_`disas'=strpos(IFLS5_nd01,"`let'") if ifls==5


gen disbad_`disas'=0
replace disbad_`disas'=0 if IFLS4_nd02==3 & ifls==4 & dis_`disas'==1
replace disbad_`disas'=0 if IFLS5_nd02==3 & ifls==5 & dis_`disas'==1
replace disbad_`disas'=1 if IFLS4_nd02==1 & ifls==4 & dis_`disas'==1
replace disbad_`disas'=1 if IFLS5_nd02==1 & ifls==5 & dis_`disas'==1
replace disbad_`disas'=. if dis_`disas'>1 & disbad_`disas'==1

replace dis_`disas'=1 if dis_`disas'>1


egen dis_`disas'_hh4=max(dis_`disas') if ifls==4, by(IFLS4_hhid07)
egen dis_`disas'_hh5=max(dis_`disas') if ifls==5, by(IFLS5_hhid14)

egen disbad_`disas'_hh4=max(disbad_`disas') if ifls==4, by(IFLS4_hhid07)
egen disbad_`disas'_hh5=max(disbad_`disas') if ifls==5, by(IFLS5_hhid14)

gen dis_`disas'_hh=.
replace dis_`disas'_hh=dis_`disas'_hh4 if ifls==4
replace dis_`disas'_hh=dis_`disas'_hh5 if ifls==5

gen disbad_`disas'_hh=.
replace disbad_`disas'_hh=disbad_`disas'_hh4 if ifls==4
replace disbad_`disas'_hh=disbad_`disas'_hh5 if ifls==5

drop dis_`disas'_hh? disbad_`disas'_hh?

la var dis_`disas'_hh "HH experienced `disas' in the past 5 years"
la var disbad_`disas'_hh "Was `disas' bad enough"


}

*Migration
gen evermig = .
replace evermig = migrate07 if ifls == 4
replace evermig = migrate14 if ifls == 5
la var evermig "Migrated cross prov lines in past"

*Earliest year of migration
gen earliestyrmigTOT1=earliestyrmig07 if ifls==4
replace earliestyrmigTOT1=earliestyrmig14 if ifls==5
egen earliestyrmigTOT=min(earliestyrmigTOT1), by(pidlink)

drop earliestyrmigTOT1

*HH economic variables

*Income
gen income=.
replace income=IFLS4_totinc if ifls==4
replace income=IFLS5_totinc if ifls==5
la var income "Household Income"

*Assets
gen assets=.
replace assets=IFLS4_totass if ifls==4
replace assets=IFLS5_totass if ifls==5
la var assets "Household Assets"

*Borrowing
gen borrowingtotal=.
replace borrowingtotal=IFLS4_totbor if ifls==4
replace borrowingtotal=IFLS5_totbor if ifls==5
la var borrowingtotal "Household borrowing"

*Savings
gen savingtotal=.
replace savingtotal=IFLS4_totsav if ifls==4
replace savingtotal=IFLS5_totsav if ifls==5
la var savingtotal "Household savings"

*Net Yearly Savings (savings - borrowing)
gen savingnettotal=.
replace savingnettotal=IFLS4_totsav-IFLS4_totbor if ifls==4
replace savingnettotal=IFLS5_totsav-IFLS5_totbor if ifls==5
la var savingnettotal "Household net savings (-borrowing)"

*Consumption
gen aggexpcons=.
replace aggexpcons=IFLS4_totcons if ifls==4
replace aggexpcons=IFLS5_totcons if ifls==5
la var aggexpcons "Household Consumption"

*Liquid assets
gen IFLS4liquidassets=.
ereplace IFLS4liquidassets=rowtotal(IFLS4_hhasssav IFLS4_hhassrec IFLS4_hhassjew IFLS4_hhassliv IFLS4_hhasscc IFLS4_fasscc IFLS4_fassliv IFLS4_fasspoul IFLS4_hhasspoul) if ifls==4

gen IFLS5liquidassets=.
ereplace IFLS5liquidassets=rowtotal(IFLS5_hhasssav IFLS5_hhassrec IFLS5_hhassjew IFLS5_hhassliv IFLS5_hhasscc IFLS5_fasscc IFLS5_fassliv IFLS5_fasspoul IFLS5_hhasspoul) if ifls==5

gen liquidassets=.
replace liquidassets=IFLS4liquidassets if ifls==4
replace liquidassets=IFLS5liquidassets if ifls==5
la var liquidassets "Household Liquid Assets"

*Non-liquid assets
gen nonliquidassets=.
replace nonliquidassets=assets-liquidassets
la var nonliquidassets "Household Non-Liquid Assets"

*House + land assets
gen homelandassets=.
ereplace homelandassets=rowtotal(IFLS4_hhasshouse IFLS4_hhassothhouse IFLS4_hhassland) if ifls==4
ereplace homelandassets=rowtotal(IFLS5_hhasshouse IFLS5_hhassothhouse IFLS5_hhassland) if ifls==5

*Non-House or land Assets
gen nonhomelandassets=assets-homelandassets

*Labor income
gen laborincome=.
replace laborincome=IFLS4_totlabin if ifls==4
replace laborincome=IFLS5_totlabin if ifls==5

*Non-Labor income
gen nonlaborincome=income-laborincome


*Inflation adjustment
merge m:1 Prov2014 using "$reploc/data/Indo_prov_cpi2014.dta", gen (_mergecpi)
drop if _mergecpi == 2

merge m:1 Prov2007 using "$reploc/data/Indo_prov_cpi2007.dta", gen (_mergecpi07)
drop if _mergecpi07 == 2

drop _mergecpi _mergecpi07

gen cpi1yrfwr=.
replace cpi1yrfwr=cpi2008_07 if ifls==4
replace cpi1yrfwr=cpi_1415 if ifls==5

gen inflation = cpi2014/cpi2007 if ifls==5
replace inflation = 1 if ifls == 4
la var inflation "Inflation rate 2008-2014"

local monvar income assets borrowingtotal savingtotal savingnettotal aggexpcons liquidassets nonliquidassets homelandassets nonhomelandassets laborincome nonlaborincome

foreach x in `monvar' {
	gen `x'_ia = .
	replace `x'_ia = `x' if ifls == 4
	replace `x'_ia = `x'*cpi2007/cpi2014 if ifls == 5
	la var `x'_ia "Inflation adjusted `x'"
}

*Per-capita adjustment

foreach x in `monvar'{
	gen `x'_pc = .
	replace `x'_pc = `x'
	replace `x'_pc = `x'/hhsize if hhsize != .
	la var `x'_pc "Per capita `x'"
}

*Monthly Income
gen mth_inc = income/12
gen mth_inc_ia = income_ia/12

*Risk preferences - module A
gen IFLS4_rbuc = .
replace IFLS4_rbuc = 1 if IFLS4_si02 == 1 & ifls == 4
replace IFLS4_rbuc = 2 if IFLS4_si04 == 1 & ifls == 4
replace IFLS4_rbuc = 3 if IFLS4_si04 == 2 & ifls == 4
replace IFLS4_rbuc = 4 if IFLS4_si05 == 1 & ifls == 4
replace IFLS4_rbuc = 5 if IFLS4_si05 == 2 & ifls == 4

gen IFLS5_rbuc = .
replace IFLS5_rbuc = 1 if IFLS5_si02 == 1 & ifls == 5
replace IFLS5_rbuc = 2 if IFLS5_si04 == 1 & ifls == 5
replace IFLS5_rbuc = 3 if IFLS5_si04 == 2 & ifls == 5
replace IFLS5_rbuc = 4 if IFLS5_si05 == 1 & ifls == 5
replace IFLS5_rbuc = 5 if IFLS5_si05 == 2 & ifls == 5

gen rbuc=.
replace rbuc=IFLS4_rbuc if ifls==4
replace rbuc=IFLS5_rbuc if ifls==5

*Flipping order of rbuc: higher numbers -> more risk averse
gen rbuc1 = .
replace rbuc1 = 1 if rbuc == 5
replace rbuc1 = 2 if rbuc == 4
replace rbuc1 = 3 if rbuc == 3
replace rbuc1 = 4 if rbuc == 2
replace rbuc1 = 5 if rbuc == 1
drop rbuc
rename rbuc1 rbuc

label var rbuc "Risk aversion buckets"


*Risk preferences - module B
gen IFLS4_rbucB = .
replace IFLS4_rbucB = 1 if IFLS4_si14 == 1 & ifls == 4
replace IFLS4_rbucB = 2 if IFLS4_si14 == 2 & ifls == 4
replace IFLS4_rbucB = 3 if IFLS4_si15 == 1 & ifls == 4
replace IFLS4_rbucB = 4 if IFLS4_si15 == 2 & ifls == 4
replace IFLS4_rbucB = 5 if IFLS4_si12 == 1 & ifls == 4

gen IFLS5_rbucB = .
replace IFLS5_rbucB = 1 if IFLS5_si14 == 1 & ifls == 5
replace IFLS5_rbucB = 2 if IFLS5_si14 == 2 & ifls == 5
replace IFLS5_rbucB = 3 if IFLS5_si15 == 1 & ifls == 5
replace IFLS5_rbucB = 4 if IFLS5_si15 == 2 & ifls == 5
replace IFLS5_rbucB = 5 if IFLS5_si12 == 1 & ifls == 5

gen rbucB=.
replace rbucB=IFLS4_rbucB if ifls==4
replace rbucB=IFLS5_rbucB if ifls==5

gen rbucB1 = .
replace rbucB1 = 1 if rbucB == 5
replace rbucB1 = 2 if rbucB == 4
replace rbucB1 = 3 if rbucB == 3
replace rbucB1 = 4 if rbucB == 2
replace rbucB1 = 5 if rbucB == 1
drop rbucB
rename rbucB1 rbucB

*Rbuc without gamble averse
gen rbuc_nga = .
replace rbuc_nga = rbuc if rbuc != 5

*Gamble averse and other refuments
gen gambleaverse=.
replace gambleaverse=1 if rbuc==5
replace gambleaverse=0 if rbuc<5

gen gambleaverserbucBund5=.
replace gambleaverserbucBund5=0 if gambleaverse==0 | rbucB==5
replace gambleaverserbucBund5=1 if gambleaverse==1 & rbucB<5

gen b3acp3=.
replace b3acp3=IFLS4_b3acp3 if ifls==4
replace b3acp3=IFLS5_b3acp3 if ifls==5

gen rav_score=.
replace rav_score=IFLS4_rav_score if ifls==4
replace rav_score=IFLS5_rav_score if ifls==5

gen rbucgaref1a=rbuc if gambleaverserbucBund5==0
gen rbucgaref2a=rbuc if (b3acp3<=2 | gambleaverse==0 | b3acp3==.)
gen rbucgaref3a=rbuc if (rav_score>1 | gambleaverse==0 | rav_score==.)
gen rbucgaref4a=rbuc if (religiosity<4 | muslim==0 | gambleaverse==0 | religiosity==. | muslim==.)

gen rbuc_garefined=rbuc if rbucgaref1a!=. & rbucgaref2a!=. & rbucgaref3a!=. & rbucgaref4a!=.

egen gambleaverselevels=total(rbuc), by(pidlink)

gen rbuc_garefineddbGA=rbuc if rbucgaref1a!=. & rbucgaref2a!=. & rbucgaref3a!=. & rbucgaref4a!=. & gambleaverselevels<10

*Binarized measure of risk aversion
gen riskaverse = .
replace riskaverse = 1 if rbuc == 3 | rbuc == 4 | rbuc==5
replace riskaverse = 0 if rbuc == 1 | rbuc==2
la var riskaverse "Risk Averse"

tempfile dataint
save "`dataint'"

*Structural estimation

import excel "$reploc/data/Indo_bounds", clear first ///Matlab code to construct these bounds available upon request

*Put + and - infinity in right format
foreach var of varlist * {

local hola=substr("`var'",-4,2)

if "`hola'"=="UB" {

replace `var'=9999999 if `var'==65535

}

if "`hola'"=="LB" {

replace `var'=-9999999 if `var'==65535

}

}

merge 1:1 pidlink ifls using  "`dataint'", gen(_mergeSTRUCEST1)

local xvars income_pc_monthly NarrowBracket

foreach x in `xvars' {

gen `x'normUBA=`x'_UB_A 
gen `x'normLBA=`x'_LB_A 
gen `x'normUBB=`x'_UB_B 
gen `x'normLBB=`x'_LB_B 

replace `x'_UB_A=. if `x'normUBA<`x'normLBA
replace `x'_LB_A=. if `x'normUBA<`x'normLBA
replace `x'_UB_B=. if `x'normUBB<`x'normLBB
replace `x'_LB_B=. if `x'normUBB<`x'normLBB

}



*Find intersection of two ranges
local xvars income_pc_monthly NarrowBracket

foreach x in `xvars' {

capture drop compat_bds`x' `x'_LB_COM `x'_UB_COM

gen compat_bds`x'=.
gen `x'_LB_COM=.
gen `x'_UB_COM=. 


replace compat_bds`x'=0 if `x'_LB_A>`x'_UB_B | `x'_LB_B>`x'_UB_A

replace compat_bds`x'=1 if `x'_LB_A<=`x'_UB_B & `x'_LB_B<=`x'_UB_A & `x'_LB_A<=`x'_LB_B
replace `x'_LB_COM=`x'_LB_B if `x'_LB_A<=`x'_UB_B & `x'_LB_B<=`x'_UB_A & `x'_LB_A<=`x'_LB_B
replace `x'_UB_COM=`x'_UB_A if `x'_LB_A<=`x'_UB_B & `x'_LB_B<=`x'_UB_A & `x'_LB_A<=`x'_LB_B


replace compat_bds`x'=1 if `x'_LB_A<=`x'_UB_B & `x'_LB_B<=`x'_UB_A & `x'_LB_B<=`x'_LB_A 
replace `x'_LB_COM=`x'_LB_A if `x'_LB_A<=`x'_UB_B & `x'_LB_B<=`x'_UB_A & `x'_LB_B<=`x'_LB_A 
replace `x'_UB_COM=`x'_UB_B if `x'_LB_A<=`x'_UB_B & `x'_LB_B<=`x'_UB_A & `x'_LB_B<=`x'_LB_A 
}


*Sample selection
drop if ifls == .
drop if pidlink == ""
drop if regexm(pidlink, "[^0-9]")

drop if biryear_impl == . | biryear_impl==0

sort pidlink ifls
quietly by pidlink: gen panel = cond(_N==1,0,_n)
replace panel = 1 if panel == 2
la var panel "appear in both waves"

*Drop missing/not correct birthyr
drop if birprov_impl<11 | (birprov_impl>=19 & birprov_impl<=30) | (birprov_impl>=36 & birprov_impl<=50) | (birprov_impl>=54 & birprov_impl<=60) | (birprov_impl>=65 & birprov_impl<=70) | (birprov_impl>=75 & birprov_impl<=80) | (birprov_impl>=83 & birprov_impl<=90) | (birprov_impl>=93 & birprov_impl!=.)
